import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:huajianshe_app/base/BaseState.dart';
import 'package:huajianshe_app/base/Refresher.dart';
import 'package:huajianshe_app/common/space.dart';
import 'package:huajianshe_app/network/Api.dart';
import 'package:huajianshe_app/pages/circle/model/CircleModel.dart';
import 'package:huajianshe_app/widget/appbar/CAppBar.dart';

import 'CircleItem.dart';

class CircleListPageWithAppBar extends StatefulWidget {

  final int? type;
  final String? title;

  const CircleListPageWithAppBar(this.type, this.title, {Key? key,}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _CircleListPageWithAppBarState();
  }
}

class _CircleListPageWithAppBarState extends BaseState<CircleListPageWithAppBar> {

  int page = 1;

  @override
  void initState() {
    super.initState();
    refresher = Refresher<CircleListData>(this);
    load(false);
  }

  load(bool doNotShowProgressBar) {
    var data = {
      'page': page,
      'list_rows': 10,
      'get_type': widget.type
    };
    sendReq(Api.circleMoreList(), CircleListResp(), data: data, doNotShowProgressBar: doNotShowProgressBar);
  }

  @override
  void onReqSuccess<T>(T t, int code, String msg, String path) {
    if (t is CircleListResp) {
      refresher?.load(t.data?.data);
      setState(() => {});
      page++;
    }
  }

  @override
  Widget build(BuildContext context) {
    return
        Scaffold(
          backgroundColor: Colors.white,
          appBar: CAppBar(title: widget.title ?? '', backImgUrl: 'ic_black_arrow', overlayStyle: SystemUiOverlayStyle.dark,),
          body: ColoredBox(color: Colors.white,
              child: Padding(padding: EdgeInsets.all(15),
                  child: refresher?.build(buildChild())
              )
          ),
        );
  }

  Widget? buildChild() {
    Widget? child = buildEmptyPage();
    if (child != null) {
      return child;
    }

    return
      ListView.separated(
          itemBuilder: (_, index) {
            return CircleItem(refresher?.data?[index]);
          },
          separatorBuilder: (_, index) => DD(paddingLeft: 48,),
          itemCount: (refresher?.data?.length ?? 0)
      );
  }

  @override
  void onLoadMore() {
    print('load more');
    refresher?.mode = Mode.loadMore;
    load(true);
  }

  @override
  void onRefresh() {
    page = 1;
    refresher?.mode = Mode.refresh;
    load(true);
  }
}